package com.all.play.mysvnbanchone.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

/**
 * Created by 聪明一只哈 on 2018/5/17.
 * Time: 2018/5/17  14:13
 */

public class GridItemDecoration extends RecyclerView.ItemDecoration {

    private Drawable mDivider;


    private int[] attrs = new int[]{
            android.R.attr.listDivider
    };

    public GridItemDecoration(Context context, int mOrientation) {

        TypedArray typedArray = context.obtainStyledAttributes(attrs);
        mDivider = typedArray.getDrawable(0);
        typedArray.recycle();


    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        drawVertical(c, parent);
        drawHorizontal(c, parent);
    }

    private void drawHorizontal(Canvas c, RecyclerView parent) {

        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            int left = child.getLeft() - params.leftMargin;
            int right = child.getRight() + params.rightMargin + mDivider.getIntrinsicWidth();
            int top = child.getBottom() + params.bottomMargin;
            Log.i("qwqwqwa",top+"");
            int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    private void drawVertical(Canvas c, RecyclerView parent) {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {


            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
//            int left = child.getRight() + params.rightMargin;
//            int right = left + mDivider.getIntrinsicWidth();
//            int top = child.getTop() - params.topMargin;
//            Log.i("qwqwqwb",top+"~");
//            int bottom = child.getBottom() + params.bottomMargin;
            int left = child.getRight();
            int right = left + mDivider.getIntrinsicWidth();
            int top = child.getTop();
            int bottom = child.getBottom();

            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        //代表四个方向的偏移值
        int right = mDivider.getIntrinsicWidth();
        int bottom = mDivider.getIntrinsicHeight();
//        outRect.set(0,0,right,bottom);
        //获取条目在adapter中的位置
        int childposition = parent.getChildAdapterPosition(view);

        int top=view.getTop();
        Log.i("qwqwqwc",top+"");

//
//        if(isLastColum(childposition,parent)){
//            outRect.set(0,0,0,bottom);
//
//        }else if(isLastRow(childposition,parent)){
//            outRect.set(0,0,right,0);
//        }else{
//            outRect.set(0,0,right,bottom);
//        }


        if (isLastColum(childposition, parent)) {
            right = 0;

        }
        if (isLastRow(childposition, parent)) {
            bottom = 0;
        }
        outRect.set(0, 0, right, bottom);


    }


    /**
     * 是否是最后一行
     *
     * @param childposition
     * @param parent
     * @return
     */
    private boolean isLastRow(int childposition, RecyclerView parent) {

        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            //管理视图的摆放
            GridLayoutManager lm = (GridLayoutManager) parent.getLayoutManager();
            //只有gridview才有列,获得列数
            int spanCount = lm.getSpanCount();
            //条目一共有多少个
            int childCount = parent.getAdapter().getItemCount();
            //共多少个 对  共多少列  求余 =最后一个行view的个数
            int lastRowCount = childCount % spanCount;

            if (lastRowCount == 0 || lastRowCount < spanCount) {
                return true;
            }


        }

        return false;
    }

    /**
     * 是否是最后一列
     *
     * @param childposition
     * @param parent
     * @return
     */
    private boolean isLastColum(int childposition, RecyclerView parent) {

        RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            //管理视图的摆放
            GridLayoutManager lm = (GridLayoutManager) parent.getLayoutManager();
            //只有gridview才有列,获得列数
            int spanCount = lm.getSpanCount();
            if ((childposition + 1) % spanCount == 0) {//是最后一列
                return true;
            }
        }


        return false;
    }
}
